package com.godyao.mall.seckill.repository;

import com.godyao.mall.seckill.model.SeckillActivity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

/**
 * @author godyao
 * @date 2022/4/1
 */
@Repository
public interface SeckillRepository extends JpaRepository<SeckillActivity, Long> {
    Optional<SeckillActivity> findByGoodsId(Long goodsId);

    /**
     * 动态条件分页查找
     * @param spec
     * @param pageable
     * @return
     */
    Page<SeckillActivity> findAll(Specification<SeckillActivity> spec, Pageable pageable);

    /**
     * 判断商品是否有效
     * @param goodsId
     * @return
     */
    @Query(nativeQuery = true, value = "select count(1) from t_seckill_activity where goods_id = ?1 and now() between start_time and end_time and stock > 0")
    int findGoodsInvalid(Long goodsId);
}
